package com.wb.web.controller.wwwinfo;

import com.wb.common.annotation.Anonymous;
import com.wb.common.core.controller.BaseController;
import com.wb.common.core.domain.AjaxResult;
import com.wb.common.core.page.TableDataInfo;
import com.wb.common.exception.base.BaseException;
import com.wb.common.utils.StringUtils;
import com.wb.common.vo.PositionModelVO;
import com.wb.common.vo.WwwInfoModelVO;
import com.wb.system.domain.PositionModel;
import com.wb.system.domain.WwwInfoModel;
import com.wb.system.service.IPositionService;
import com.wb.system.service.IWwwInfoService;
import com.wb.web.controller.position.vo.PositionModelCreateVO;
import com.wb.web.controller.wwwinfo.vo.WwwInfoModelCreateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @Description TODO
 * @Date 2025/7/10 15:23
 */
@RestController
@RequestMapping("wwwinfo")
@Api(tags = "网站")
public class WwwInfoController extends BaseController {
    @Autowired
    private IWwwInfoService iWwwInfoService;

    @GetMapping("/list")
    @ApiOperation("网站列表")
    public TableDataInfo list(WwwInfoModelVO wwwInfoModelVO) {
        startPage();
        List<WwwInfoModel> list = iWwwInfoService.selectWwwInfoList(wwwInfoModelVO);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(wwwInfoModel -> {
                String bannerUrl = wwwInfoModel.getBannerUrl();
                if (StringUtils.isNotEmpty(bannerUrl)) {
                    wwwInfoModel.setBannerUrlList(Arrays.asList(bannerUrl.split(",")));
                }

            });
        }
        return getDataTable(list);
    }

    @PostMapping("/add")
    @ApiOperation("网站创建")
    public AjaxResult add(@Valid @RequestBody WwwInfoModelCreateVO wwwInfoModelCreateVO) {
        WwwInfoModel wwwInfoModel = new WwwInfoModel();
        BeanUtils.copyProperties(wwwInfoModelCreateVO, wwwInfoModel);

        iWwwInfoService.save(wwwInfoModel);
        return AjaxResult.success();
    }
    @GetMapping("/getWwwInfoLeast")
    @ApiOperation("最新生效的一条网站详情")
    @Anonymous
    public AjaxResult getWwwInfoLeast() {
        WwwInfoModel wwwInfoModel = iWwwInfoService.getWwwInfoLeast();
        if(wwwInfoModel!=null){
            String bannerUrl = wwwInfoModel.getBannerUrl();
            if (StringUtils.isNotEmpty(bannerUrl)) {
                wwwInfoModel.setBannerUrlList(Arrays.asList(bannerUrl.split(",")));
            }
        }
        return AjaxResult.success(wwwInfoModel);
    }
    @GetMapping("/getWwwInfo")
    @ApiOperation("网站详情")
    @Anonymous
    public AjaxResult getWwwInfoDetail(Long id) {
        WwwInfoModel wwwInfoModel = iWwwInfoService.getWwwInfoDetail(id);
        if(wwwInfoModel!=null){
            String bannerUrl = wwwInfoModel.getBannerUrl();
            if (StringUtils.isNotEmpty(bannerUrl)) {
                wwwInfoModel.setBannerUrlList(Arrays.asList(bannerUrl.split(",")));
            }
        }
        return AjaxResult.success(wwwInfoModel);
    }

    @PostMapping("/update")
    @ApiOperation("更新网站")
    public AjaxResult update(@RequestBody WwwInfoModelCreateVO createVO) {
        WwwInfoModel wwwInfoModel = new WwwInfoModel();
        BeanUtils.copyProperties(createVO, wwwInfoModel);
     //   wwwInfoModel.setUpdateBy(getUsername());
        iWwwInfoService.updateWwwInfoById(wwwInfoModel);
        return AjaxResult.success();
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("删除网站")
    public AjaxResult batchDelete(List<Long> ids) {
        iWwwInfoService.batchDeleteWwwInfo(ids);
        return AjaxResult.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除网站")
    public AjaxResult delete(Long id) {
        iWwwInfoService.removeWwwInfoById(id);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation("删除网站All")
    public AjaxResult deleteAll(@RequestParam("ids") List<Long> ids) {
        iWwwInfoService.removeWwwInfoByIds(ids);
        return AjaxResult.success();
    }

    @PostMapping("/updateStatus")
    @ApiOperation("更新网站状态")
    public AjaxResult updateStatus(@RequestBody WwwInfoModel wwwInfoModel) {
        if (null == wwwInfoModel || StringUtils.isNull(wwwInfoModel.getId())) {
            throw new BaseException("参数对象或ID为空，更新网站状态失败");
        }
        WwwInfoModel entity = new WwwInfoModel();
        entity.setId(wwwInfoModel.getId());
        entity.setStatus(wwwInfoModel.getStatus());
        iWwwInfoService.updateStatus(entity);
        return AjaxResult.success();
    }


}
